package com.handu.luna.boss.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.handu.luna.core.domain.goods.BrandGeneralInfo;

@Controller
@RequestMapping("/goods/brandgeneralinfo/*")
public class BrandGeneralInfoController extends BaseController{
	
	
	/*@RequestMapping
	public BossModel index(){
		return createModel("goods/brand/generalinfo/index").add("brandGeneralInfoList",  new BrandGeneralInfo().findAll());
	}*/
	
	@RequestMapping
	public BossModel index(String brandName,String isUnexpired){
		
		BossModel bossModel = this.createModel("goods/brand/generalinfo/index");
				
		//第一次访问 默认选择显示过期
		if(brandName == null && isUnexpired == null) {
			isUnexpired = "show";
			bossModel.add("isUnexpired", "show");
		}
		
		if(brandName != null && !brandName.equals("")) {
			bossModel.add("brandName", brandName);
		}
		if(isUnexpired != null && !isUnexpired.equals("")) {
			bossModel.add("isUnexpired", isUnexpired);
		}
		
		bossModel.add("brandGeneralInfoList",  new BrandGeneralInfo().findAll(brandName, isUnexpired));
		return bossModel;
		
		
	}
	
	@RequestMapping(value="add")
	public BossModel add(){
		
		BrandGeneralInfo brandGeneralInfo = new BrandGeneralInfo();
		brandGeneralInfo.setDisplayed(true);
		
		return createModel("goods/brand/generalinfo/info").add("brandGeneralInfo", brandGeneralInfo)
				.add("brands", context.getBrandRepository().findAll());
	}
	
	@RequestMapping(value="edit/{id}")
	public BossModel edit(@PathVariable long id){
		return createModel("goods/brand/generalinfo/info").add("brandGeneralInfo", context.getBrandGeneralInfoRepository().findOne(id))
				.add("brands", context.getBrandRepository().findAll());
	}
	
	@RequestMapping(value="save",method = RequestMethod.POST)
	public BossModel save(@Valid @ModelAttribute("brandGeneralInfo") final BrandGeneralInfo brandGeneralInfo,final BindingResult bindingResult, RedirectAttributes redirectAttributes){
		
		if(brandGeneralInfo.getStartDate() == null) {
			bindingResult.rejectValue("startDate", "misFormat", "请输入开始日期");
		}
		if(brandGeneralInfo.getEndDate() == null) {
			bindingResult.rejectValue("endDate", "misFormat", "请输入结束日期");
		}
		
		if(brandGeneralInfo.getStartDate() != null && brandGeneralInfo.getEndDate() != null) {
			if(brandGeneralInfo.getStartDate().compareTo(brandGeneralInfo.getEndDate()) != -1) {
				bindingResult.rejectValue("startDate", "misFormat", "开始时间必须小于结束时间");
			}
		}
		
		if (bindingResult.hasErrors()) {
			return createModel("goods/brand/generalinfo/info").add("brands", context.getBrandRepository().findAll());
		}
		String message = "";
		brandGeneralInfo.init();
		if(null==brandGeneralInfo.getId()){
			message = "添加品牌"+brandGeneralInfo.getBrand().getName()+ "通用信息成功";
		}else{
			message = "修改品牌"+brandGeneralInfo.getBrand().getName()+ "通用信息成功";
		}
		context.getBrandGeneralInfoRepository().save(brandGeneralInfo);
		redirectAttributes.addFlashAttribute("message", message);
		
		return createModel("redirect:index");
	}
	
	@RequestMapping(value="delete",method= RequestMethod.POST)
	public BossModel delete(long id){
		context.getBrandGeneralInfoRepository().delete(id);
		return createModel("goods/brand/generalinfo/generalinfo_table").add("brandGeneralInfoList",new BrandGeneralInfo().findAll());
	}
	
	
	@InitBinder("brandGeneralInfo")
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true)); 
	}
	
	
	
}
